package com.gitee.zhanyao.file;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 本地文件目录操作
 *
 * @author zhanyao
 */
@Slf4j
public class LocalFileStrategy implements DirectoryStrategy, FileStrategy {

    @Override
    public boolean createDirectory(@NonNull String directory) {
        File file = new File(directory);
        if (!file.exists()) {
            return file.mkdirs();
        }
        return true;
    }

    @Override
    public boolean hasDirectory(String directory) {
        File file = new File(directory);
        return file.exists();
    }

    @Override
    public boolean renameDirectory(@NonNull String oldDirectory, @NonNull String newDirectory) {
        File from = new File(oldDirectory);
        if (from.exists()) {
            File to = new File(newDirectory);
            return from.renameTo(to);
        } else {
            log.error("{} 文件夹不存在", oldDirectory);
            return false;
        }
    }

    @Override
    public void deleteDirectory(String directory) throws IOException {
        Path path = Paths.get(directory);
        Files.delete(path);
    }

}
